//
//  XMPicwordSearchViewController.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/15.
//

#import "XMPicwordSearchViewController.h"
#import "XMPicwordSearchView.h"
#import "XMNetwork+find.h"
#import "XMNetwork+mine.h"
#import "XMPicwordModel.h"
#import "XMFriendModel.h"
#import "XMPicwordDetailViewController.h"
#import "XMUserHomePageViewController.h"

@interface XMPicwordSearchViewController () <XMPicwordSearchViewDelegate>

@property (nonatomic, strong) XMPicwordSearchView *searchView;

@property (nonatomic, copy) NSString *searchKey;

@property (nonatomic, copy) NSString *screenKeyOne;
@property (nonatomic, copy) NSString *screenKeyTwo;

@property (nonatomic, assign) int searchType;
@property (nonatomic, assign) int page;

@end

@implementation XMPicwordSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    _screenKeyOne = @"";
    _screenKeyTwo = @"1";
    [self.view addSubview:self.searchView];
}

//图文搜索
- (void)picSearchWithKey:(NSString *)searchKey {
    XMWeakSelf;
    [_Network picwordSearchWithPage:[NSString stringWithFormat:@"%d", _page]
                                key:searchKey
                           timeType:_screenKeyTwo  //时间段
                       isByTimesort:_screenKeyOne  //排序
                            success:^(id  _Nonnull responseObject) {
        NSArray *modelArr = [NSArray yy_modelArrayWithClass:[XMPicwordModel class] json:[responseObject safeArrayForKey:@"data"]];
        [weakSelf.searchView setContentWithPicwordModelArr:modelArr
                                                      page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//用户搜索
- (void)userSearchWithKey:(NSString *)searchKey {
    XMWeakSelf;
    [_Network findUserSearchWithSearchKey:searchKey
                                     page:[NSString stringWithFormat:@"%d", _page]
                                  success:^(id  _Nonnull responseObject) {
        NSArray *userModelArr = [NSArray yy_modelArrayWithClass:[XMFriendModel class] json:[responseObject safeArrayForKey:@"data"]];
        [weakSelf.searchView setContentWithUserModelArr:userModelArr
                                                   page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//点赞
- (void)picwordLikedWithPicwordId:(NSString *)picwordId {
    XMWeakSelf;
    [_Network videoLikedWithVideoId:picwordId
                            success:^(NSError * _Nonnull error) {
        [weakSelf refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//关注用户
- (void)userAttentionWithUserId:(NSString *)userId {
    XMWeakSelf;
    [_Network friendAttentionWithUserId:userId success:^(id  _Nonnull responseObject) {
        [weakSelf  refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
- (void)refreshData {
    _page = 0;
    if (_searchType == 0) {
        _page = 1;
        [self picSearchWithKey:_searchKey];
    }
    else {
        _page = 0;
        [self userSearchWithKey:_searchKey];
    }
}

- (void)loadMoreData {
    _page += 0;
    if (_searchType == 0) {
        [self picSearchWithKey:_searchKey];
    }
    else {
        [self userSearchWithKey:_searchKey];
    }
}

- (void)picwordSearchWithSearchKey:(NSString *)searchKey
                        searchType:(int)searchType {
    _searchKey = searchKey;
    _searchType = searchType;
    if (searchType == 0) {
        _page = 1;
        _screenKeyOne = @"";
        _screenKeyTwo = @"1";
        [self picSearchWithKey:searchKey];
    }
    else {
        _screenKeyOne = @"";
        _page = 0;
        [self userSearchWithKey:searchKey];
    }
}

//筛选
- (void)screenWithSearchKey:(NSString *)screenKeyOne
               screenKeyTwo:(NSString *)screenKeyTwo {
    _screenKeyOne = screenKeyOne;
    _screenKeyTwo = screenKeyTwo;
    [self refreshData];
}

//查看详情
- (void)checkPicwordDetailWithPicwordId:(NSString *)picwordId {
    XMPicwordDetailViewController *detailVC = [[XMPicwordDetailViewController alloc] init];
    detailVC.picwordId = picwordId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

//查看用户详情
- (void)checkUserDetailWithUserId:(NSString *)userId {
    XMUserHomePageViewController *homePageVC = [[XMUserHomePageViewController alloc] init];
    homePageVC.userId = userId;
    [self.navigationController pushViewController:homePageVC animated:YES];
}

#pragma mark - Init
- (XMPicwordSearchView *)searchView {
    if (!_searchView) {
        _searchView = [[XMPicwordSearchView alloc] initWithFrame:self.view.bounds];
        _searchView.delegate = self;
    }
    return _searchView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
